
package com.dps.omc.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.utils.SqlWarnAnalyzer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.omc.model.SqlWarnKeyWord;
import com.dps.omc.service.ISqlWarnKeyWordService;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class SqlWarnKeyWordAction extends AbstractAdminController<ISqlWarnKeyWordService>{

    @Autowired
    private ISqlWarnKeyWordService sqlWarnKeyWordService;
    @Autowired
    private SqlWarnAnalyzer sqlWarnAnalyzer;
    
    @RequestMapping(value="/sqlwarnkeyword/list")
    public String list(SqlWarnKeyWord sqlWarnKeyWord,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(sqlWarnKeyWord); 	
    	BizData4Page<SqlWarnKeyWord> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("sqlWarnKeyWord", sqlWarnKeyWord);
    	return "/module/sqlwarnkeyword/list";
    }    
    
    @RequestMapping(value="/sqlwarnkeyword/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("sqlWarnKeyWord", new SqlWarnKeyWord());
        return "module/sqlwarnkeyword/edit";
    }    
    
    @RequestMapping(value="/sqlwarnkeyword/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	SqlWarnKeyWord sqlWarnKeyWord = sqlWarnKeyWordService.fetch(id);
    	model.addAttribute("sqlWarnKeyWord", sqlWarnKeyWord);
        return "module/sqlwarnkeyword/edit";
    }    
    
    @RequestMapping(value="/sqlwarnkeyword/save")
    @ResponseBody
    public String save(HttpServletRequest request,SqlWarnKeyWord sqlWarnKeyWord){
		boolean isAddNew = StringUtils.isBlank(sqlWarnKeyWord.getId())?true:false;
		if(isAddNew)
		{
			sqlWarnKeyWord.setId(IdGenerator.createNewId());
			sqlWarnKeyWordService.add(sqlWarnKeyWord);
		}
		else
		{
			sqlWarnKeyWordService.update(sqlWarnKeyWord);
		}
        return JsonResult.operateSuccessClose(Constants.MAIN_TAB,"SQL危险关键字已修改,但未生效，请重载系统参数！").toString();
//        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/sqlwarnkeyword/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	sqlWarnKeyWordService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }

    @RequestMapping(value="/sqlwarnkeyword/reload")
    @ResponseBody
    public String reload(String ids,HttpServletRequest request){

        sqlWarnAnalyzer.reload();
        return JsonResult.operateSuccess(Constants.MAIN_TAB).toString();
    }
	
    protected Map getQueryMap(SqlWarnKeyWord sqlWarnKeyWord)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("keyWord", sqlWarnKeyWord.getKeyWord());		
		conditions.put("createDate", sqlWarnKeyWord.getCreateDate());		
		conditions.put("createrName", sqlWarnKeyWord.getCreaterName());		
		conditions.put("createrId", sqlWarnKeyWord.getCreaterId());		
    	return conditions;
    }

    @Override
    protected ISqlWarnKeyWordService getMainService() {
        return sqlWarnKeyWordService;
    }

    @Override
    protected String getMainObjName() {
        return "sqlwarnkeyword";
    }

    @Override
    protected String getViewTitle() {
        return "sqlwarnkeyword";
    }
}
